<template>
  <div>
    <div class="content">
      <div class="content_input">
        <div class="title">
          <p>物资管理系统</p>
        </div>
        <el-input
          v-model="account"
          clearable
          placeholder="用户名"
          @keyup.enter.native="submit"
          prefix-icon="el-icon-user-solid"
          ref="account"
        ></el-input>
        <el-input
          v-model="password"
          clearable
          show-password
          placeholder="密码"
          @keyup.enter.native="submit"
          prefix-icon="el-icon-lock"
          ref="password"
        ></el-input>
        <div class="content_button">
          <el-button type="primary" @click="submit">登录</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
let loginApi = require("@/api/login");
import Storage from "@/util/localStorage";
let l_storage = new Storage();

export default {
  data() {
    return {
      account: "",
      password: "",
    };
  },
  methods: {
    async loginIn(data) {
      let res = await loginApi.loginIn(data);
      res.data.account = this.account;
      this.$store.commit("LOGIN_IN", res.data);
      this.$router.push("/");
    },
    submit() {
      let obj = {
        id: this.account,
        psd: this.password,
      };
      if (!this.account && !this.password) {
        this.$notify.error({
          title: "错误",
          message: "用户名和密码不能为空",
        });
        return;
      } else if (!this.password) {
        this.$notify.error({
          title: "错误",
          message: "密码不能为空",
        });
        return;
      } else if (!this.account) {
        this.$notify.error({
          title: "错误",
          message: "用户名不能为空",
        });
        return;
      }
      this.loginIn(obj);
    },
    /* 登录框失去焦点时触发 */
    async checkName() {
      /* checkName的作用：在用户输完账号后，直接去后台获取用户密码存储在Session中，
        而当用户输入完密码后可以直接与Session中的数据进行比对，加快了访问速度；
        因而，如果用户先输入的是密码，则不触发checkName
      */
      /* let res = await loginApi.checkName(this.account);
      debugger; */
    },
  },
  created() {
    /* 判断vuex中有没有上次登录保存下来的用户名 */
    this.account = l_storage.get("account") || "";
  },
  mounted() {
    /* 如果account有初始值，则密码框获取焦点，反之用户名框获取焦点 */
    this.account == ""
      ? this.$refs["account"].focus()
      : this.$refs["password"].focus();
  },
};
</script>

<style scoped>
.content {
  width: 500px;
  height: 350px;
  box-sizing: border-box;
  padding: 0 50px;
  border-radius: 5px;
  box-shadow: 0px 2px 12px 0px rgba(105, 105, 105, 0.07);
  background: rgba(255, 255, 255, 0.7);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  overflow: hidden;
  transition: 1.5s;
}

.content_input {
  width: 300px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.content_button {
  margin-top: 10px;
}

.el-input {
  margin-bottom: 25px;
}

.title {
  text-align: center;
  font-size: 27px;
  margin-bottom: 30px;
  letter-spacing: 0.1rem;
  font-weight: 700;
  color: #38393b;
}

.el-button--primary {
  width: 100%;
}
</style>